package online.yysh.charging.view;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PathMeasure;
import android.os.Handler;
import android.os.Message;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import online.yysh.charging.R;
import online.yysh.charging.utils.DateUtil;

/**
 * 充电球体view
 */
public class ChargingView extends View {

    //开始绘图
    private final int START_DRAW = 200;
    //停止绘图
    private final int STOP_DRAW = 201;
    //时间画笔
    private Paint textPaint;
    //内圆画笔
    private Paint inCirclePaint;
    //外圆画笔
    private Paint outPaint;
    //线条画笔
    private Paint linePaint;

    //内圆中显示的时间
    private String time;
    //时间text大小
    private int textSize;
    //内圆半径
    private int inCircleRadius;
    //字体颜色
    private int textColor;
    //外圆半径
    private int outCircleRadius;
    //内圆颜色
    private int inCircleColor;
    //外圆颜色
    private int outCircleColor;
    //线颜色
    private int lineColor;
    //线长度
    private int lineLength;

    private Bitmap lineBitmap;
    private Path path;
    //计算路径
    private PathMeasure pathMeasure;
    //图片矩阵
    private Matrix lineMatrix;
    private float distanceRatio = 0;

    public ChargingView(Context context) {
        this(context, null);
    }

    public ChargingView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public ChargingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray ta = context.obtainStyledAttributes(attrs,R.styleable.ChargingView);
        textSize = ta.getInt(R.styleable.ChargingView_textSize,88);
        textColor = ta.getResourceId(R.styleable.ChargingView_textColor, R.color.black);
        inCircleRadius = ta.getInt(R.styleable.ChargingView_inCircleRadius,210);
        outCircleRadius = ta.getInt(R.styleable.ChargingView_outCircleRadius,300);
        inCircleColor = ta.getResourceId(R.styleable.ChargingView_inCircleColor,R.color.white);
        outCircleColor = ta.getResourceId(R.styleable.ChargingView_outCircleColor,R.color.blue2);
        lineColor = ta.getResourceId(R.styleable.ChargingView_lineColor,R.color.white);
        lineLength = ta.getResourceId(R.styleable.ChargingView_lineLength,300);

        initPaint();
        initBitmap();

        handler.sendEmptyMessage(START_DRAW);
    }

    private void initBitmap() {
        lineBitmap = BitmapFactory.decodeResource(getResources(),R.mipmap.line);
        if (lineBitmap == null) {
            return;
        }
        int width = lineBitmap.getWidth();
        int height = lineBitmap.getHeight();
        Matrix matrix = new Matrix();
        matrix.preScale(0.5f, 0.5f);
        lineBitmap = Bitmap.createBitmap(lineBitmap, 0, 0, width, height, matrix, false);
        path = new Path();
        path.addCircle(0,0,inCircleRadius,Path.Direction.CW);
        pathMeasure = new PathMeasure(path,false);
        lineMatrix = new Matrix();
    }

    /**
     * 初始化画笔
     */
    private void initPaint() {
        textPaint = new Paint();
        textPaint.setColor(getResources().getColor(textColor));
        textPaint.setTextSize(textSize);
        textPaint.setStyle(Paint.Style.FILL);
        textPaint.setAntiAlias(true);

        inCirclePaint = new Paint();
        inCirclePaint.setColor(getResources().getColor(inCircleColor));
        inCirclePaint.setStyle(Paint.Style.FILL);
        inCirclePaint.setAntiAlias(true);

        outPaint = new Paint();
        outPaint.setColor(getResources().getColor(outCircleColor));
        outPaint.setStyle(Paint.Style.FILL);
        outPaint.setAntiAlias(true);

        linePaint = new Paint();
        linePaint.setColor(getResources().getColor(lineColor));
        linePaint.setStrokeWidth(2);
        linePaint.setStyle(Paint.Style.STROKE);
        linePaint.setAntiAlias(true);
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(@NonNull Message msg) {
            switch (msg.what) {
                case START_DRAW:
                    time = DateUtil.getTime();
                    handler.sendEmptyMessageDelayed(START_DRAW,1000);
                    break;
                case STOP_DRAW:

                    break;
            }
        }
    };

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        int cx = getMeasuredWidth()/2;
        int cy = getMeasuredHeight() / 2;
        canvas.drawCircle(cx, cy, outCircleRadius, outPaint);


//        canvas.translate(getMeasuredWidth() / 2, getMeasuredHeight() / 2);
//        lineMatrix.reset();
//        distanceRatio += 0.006f;
//        if (distanceRatio >=1){
//            distanceRatio = 0;
//        }
//        float[] pos = new float[2];//记录位置
//        float[] tan = new float[2];//记录切点坐标
//        float distance = pathMeasure.getLength() * distanceRatio;
//        pathMeasure.getPosTan(distance,pos,tan);
//        //        tan0代表 cos tan[1] sin
//        float degree = (float) (Math.atan2(tan[1], tan[0]) * 180 / Math.PI); //计算图片本身要旋转的角度
//        lineMatrix.postRotate(degree, lineBitmap.getWidth() / 2, lineBitmap.getHeight() / 2); //设置旋转角度和旋转中心
//        //这里要将设置到图片的中心点
//        lineMatrix.postTranslate(pos[0] - lineBitmap.getWidth() / 2, pos[1] - lineBitmap.getHeight() / 2);
//        canvas.drawBitmap(lineBitmap, lineMatrix, linePaint);


        canvas.drawCircle(cx, cy, inCircleRadius, inCirclePaint);
        canvas.translate(cx, cy);
        float textWidth = textPaint.measureText(time);
        float baseLineY = Math.abs(textPaint.ascent() + textPaint.descent()) / 2;
        canvas.drawText(time, -textWidth / 2, baseLineY, textPaint);
        invalidate();
    }



}
